/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.table;

import com.jspx.boot.sign.YesNoType;
import com.jspx.json.JSONObject;
import com.jspx.sober.annotation.Column;
import com.jspx.sober.annotation.IDType;
import com.jspx.sober.annotation.Id;
import com.jspx.sober.annotation.Table;
import com.jspx.sober.table.OperateTable;
import com.jspx.utils.DateUtil;
import com.jspx.utils.StringUtil;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2010-10-15
 * Time: 9:53:18
 * 这里是一个消息存放中心
 * 所有的应用在这里起数据然后发送
 *
 */
@Table(name = "jspx_talk_message", caption = "站点消息",cache = true)
public class TalkMessage extends OperateTable {


    //0:无错误;1:站点信息错误;2:及时消息发送错误;3:邮件发送失败;4:手机发送失败
    @Id(auto = true, type = IDType.serial)
    @Column(caption = "ID", notNull = true)
    private long id = 0;

    //当前收件人ID, 这里是根据messageType 来判断处理方式,例如 为聊天室，那么这里将为聊天室ＩＤ,
    @Column(caption = "收件id", notNull = true)
    private long toId = 0;

    //当前收件人姓名
    @Column(caption = "收件姓名", length = 100, notNull = true)
    private String toName = StringUtil.empty;

    //格式[1111:name];[12321:name2]; ,本消息发送后将会接收的所有人，
    // 这里根据UI用户选择表达式得到，可以是部门或者其他
    @Column(caption = "收件人", length = 2000, notNull = true)
    private String sendTo = StringUtil.empty;

    @Column(caption = "标题", length = 200, notNull = true)
    private String title = StringUtil.empty;

    @Column(caption = "内容", length = 1000, notNull = true)
    private String content = StringUtil.empty;

    @Column(caption = "站点消息",option = "0:否;1:是",notNull = true)
    private int toSite = YesNoType.YES;

    @Column(caption = "及时消息",option = "0:否;1:是", notNull = true)
    private int toTalk = YesNoType.NO;

    @Column(caption = "发到手机",option = "0:否;1:是", notNull = true)
    private int toPhone = YesNoType.NO;

    @Column(caption = "发到邮件", notNull = true)
    private int toMail = 0;  //是否发到邮件

    @Column(caption = "消息服务", notNull = true)
    private int toServer = 0;  //是否发到邮件

    //支持多个手机号,格式aaaaa;bbbbb;ccccc;
    @Column(caption = "手机号码",length = 250, notNull = false)
    private String phones = StringUtil.empty;  //手机列表

    //支持多个邮件
    @Column(caption = "邮件列表", length = 250, notNull =false)
    private String mails = StringUtil.empty;

    //0:系统将自动处理,IM方式或为站点消息，
    //1:聊天室  得到聊天室分组人员，发送到分组里边
    //2:公告，不发送，不删除，但都会显示
    //3:管理员提示，不发送，不删除，但针对管理员都会显示
    //4:草稿，不处理
    //5:收件箱，已经收到的信息
    //6:已发邮件，自己已经发生的历史信息
    //7:垃圾箱，已经删除，但还保留的信息
    //10:系统命令，保留，目前不处理
    @Column(caption = "消息类型", option = "0:私人消息;1:聊天室;2:公告;3:管理员提示;4:草稿;5:收件箱;6:已发信息;7:垃圾箱;10:系统命令:", notNull = true)
    private int messageType = 0;

    //0:未读 大于 0 为读的次数
    @Column(caption = "阅读次数", notNull = true, defaultValue = "0")
    private int readTimes = 0;

    @Column(caption = "附件", length = 1200, dataType = "isLengthBetween(0,250)", notNull = false)
    private String attachments = StringUtil.empty;

    @Column(caption = "站点消息",option = "0:否;1:是",notNull = true)
    private int siteSend = YesNoType.NO;

    @Column(caption = "及时消息",option = "0:否;1:是", notNull = true)
    private int talkSend = YesNoType.NO;

    @Column(caption = "邮件发送成功",option = "0:否;1:是", notNull = true)
    private int mailSend =  YesNoType.NO;  //是否发到邮件

    @Column(caption = "手机发送成功",option = "0:否;1:是", notNull = true)
    private int phoneSend = YesNoType.NO;

    @Column(caption = "消息服务",option = "0:否;1:是", notNull = true)
    private int serverSend = YesNoType.NO;

    //默认为立刻发送 ,
    @Column(caption = "发送时间")
    private Date sendDate = new Date();

    //其中一个返回已经读，那么本条信息就表示已经成功,其他的就不用在发送
    //这里用 to 和 send 比较，如果都成功就得到标志， 这里是一个总开关,当这里为1 就结束不在发送
    @Column(caption = "是否已经发送", option = "0:还没有发送;1:已经发送完成", notNull = true)
    private int completed = 0;

    //如果已经发生结束,就标识,发送的信息并不删除,将作为历史保存到其他表，以备后边历史查询，和分析垃圾信息
    @Column(caption = "删除标志",option = "0:否;1:是", notNull = true)
    private int removed = YesNoType.NO;

    /*
     //2:公告，不发送，不删除，但都会显示
     //3:管理员提示，不发送，不删除，但针对管理员都会显示
     针对 2,3：这里的数据将作为，到期自动删除时间
     */
    @Column(caption = "删除时间")
    private Date removedDate = DateUtil.addYear(1);

    //如果对方已经读过内容，那么返回一个已读的消息，正对站点消息，和手机短信类
    @Column(caption = "返回已读",option = "0:否;1:请求已读;2:请求", notNull = true)
    private int requestType = 0;

    //0:无错误;1:站点信息错误;2:及时消息发送错误;3:邮件发送失败;4:手机发送失败
    @Column(caption = "错误")
    private int errorCode = 0;

    @Column(caption = "错误消息", length = 250)
    private String errorMessage = StringUtil.empty;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getToId() {
        if (toId == 0 && !StringUtil.isNULL(sendTo))
            return StringUtil.toLong(StringUtil.substringBetween(sendTo, "[", ":"));
        return toId;
    }

    public void setToId(long toId) {
        this.toId = toId;
    }

    public String getToName() {
        if (!StringUtil.hasLength(toName) && !StringUtil.isNULL(sendTo))
            return StringUtil.substringBetween(sendTo, ":", "]");
        return toName;
    }

    public int getToSite() {
        return toSite;
    }

    public void setToSite(int toSite) {
        this.toSite = toSite;
    }

    public void setToName(String toName) {
        this.toName = toName;
    }

    public boolean check() {
        return toSite + toMail + toPhone > 0;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getMessageType() {
        return messageType;
    }

    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }

    public int getCompleted() {
        return completed;
    }

    public void setCompleted(int completed) {
        this.completed = completed;
    }

    public String getSendTo() {
        return sendTo;
    }

    public void setSendTo(String sendTo) {
        this.sendTo = sendTo;
    }

    public void putSendTo(long id, String name) {
        this.sendTo = "[" + id + ":" + name + "]";
        this.toId = id;
        this.toName = name;
    }

    public void appendSendTo(long id, String name) {
        this.sendTo = this.sendTo + "[" + id + ":" + name + "];";
        this.toId = id;
        this.toName = name;
    }

    public String toString() {
        JSONObject json = new JSONObject();
        json.put("messageType", messageType);
        json.put("sendTo", sendTo);
        json.put("putUid", putUid);
        json.put("putName", putName);
        json.put("content", content);
        json.put("createDate", DateUtil.toString(createDate, DateUtil.FULL_ST_FORMAT));
        return json.toString(1);
    }

    public int getReadTimes() {
        return readTimes;
    }

    public void setReadTimes(int readTimes) {
        this.readTimes = readTimes;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getToPhone() {
        return toPhone;
    }

    public void setToPhone(int toPhone) {
        this.toPhone = toPhone;
    }

    public String getPhones() {
        return phones;
    }

    public void setPhones(String phones) {
        this.phones = phones;
    }

    public int getToMail() {
        return toMail;
    }

    public void setToMail(int toMail) {
        this.toMail = toMail;
    }

    public String getMails() {
        return mails;
    }

    public void setMails(String mails) {
        this.mails = mails;
    }

    public int getRemoved() {
        return removed;
    }

    public void setRemoved(int removed) {
        this.removed = removed;
    }

    public int getToTalk() {
        return toTalk;
    }

    public void setToTalk(int toTalk) {
        this.toTalk = toTalk;
    }

    public String getAttachments() {
        return attachments;
    }

    public void setAttachments(String attachments) {
        this.attachments = attachments;
    }

    public int getSiteSend() {
        return siteSend;
    }

    public void setSiteSend(int siteSend) {
        this.siteSend = siteSend;
    }

    public int getTalkSend() {
        return talkSend;
    }

    public void setTalkSend(int talkSend) {
        this.talkSend = talkSend;
    }

    public int getMailSend() {
        return mailSend;
    }

    public void setMailSend(int mailSend) {
        this.mailSend = mailSend;
    }

    public int getPhoneSend() {
        return phoneSend;
    }

    public void setPhoneSend(int phoneSend) {
        this.phoneSend = phoneSend;
    }

    public int getRequestType() {
        return requestType;
    }

    public void setRequestType(int requestType) {
        this.requestType = requestType;
    }

    public Date getRemovedDate() {
        return removedDate;
    }

    public void setRemovedDate(Date removedDate) {
        this.removedDate = removedDate;
    }

    public Date getSendDate() {
        return sendDate;
    }

    public void setSendDate(Date sendDate) {
        this.sendDate = sendDate;
    }

    public int getToServer() {
        return toServer;
    }

    public void setToServer(int toServer) {
        this.toServer = toServer;
    }

    public int getServerSend() {
        return serverSend;
    }

    public void setServerSend(int serverSend) {
        this.serverSend = serverSend;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    /**

     * @return 判断对应的标记是否发送结束
     */
    public boolean checkCompleted() {
        return getToSite() == getSiteSend()
                && getToMail() == getToMail()
                && getToPhone() == getToPhone()
                && getToTalk() == getToTalk()
                && getToServer() == getServerSend();
    }

}